//
// Created by zhuzonghui on 2022/12/17.
//
#include "../../header/service/SemesterService.h"
#include <mysql.h>
#include "../../header/tools/MYSQL_TOOLS.h"

Semester *getSemester(MYSQL_ROW column) {
    Semester *semester = new Semester;
    semester->setSemesterId(*column[0] - 48);
    semester->setSemesterName(column[1]);
    return semester;
}

list<Semester *> *wzy_get_semester_list() {
    string sql = "select * from semester";
    list<Semester *> *semesterList = new list<Semester *>;
    try {
        MYSQL_RES *pRes = wzy_mysql_query(sql);
        MYSQL_ROW column;
        while ((column = mysql_fetch_row(pRes))) {
            semesterList->push_back(getSemester(column));
        }
        return semesterList;
    } catch (...) {
        return semesterList;
    }
}

void wzy_add_semester(char *semesterName) {
    string sql = "select * from semester where semester_name= '" + (string) semesterName + "'";
    MYSQL_RES *pRes = wzy_mysql_query(sql);
    my_ulonglong i = pRes->row_count;
    if (i > 0) {
        throw -2;
    }
    sql = "insert into semester(semester_name) values('" + (string) semesterName + "')";
    try {
        wzy_mysql_query(sql);
    } catch (...) {
        throw -1;
    }
}

